package com.example.onlineeducation.data.constant

import androidx.annotation.*
import com.example.onlineeducation.R
import com.yc.jetpacklib.extension.ycIsEmpty
import com.yc.jetpacklib.extension.ycIsNotEmpty
import java.text.SimpleDateFormat
import java.util.*

/**
 * 线下学习活动状态
 */
object OfflineStudyLiveState {

    const val ONLINE = 1//上线
    const val OFFLINE = 2//下线

    @IntDef(ONLINE, OFFLINE)
    @kotlin.annotation.Retention(AnnotationRetention.SOURCE)
    annotation class OnlineState //1-上线 2-下线



    /**
     * 活动状态/签到状态 异常
     */
    const val ERROR = -1

    /**
     * 活动未开始
     */
    const val STUDY_LIVE_NOT_STARTED = 1

    /**
     * 活动进行中
     */
    const val STUDY_LIVE_IN_PROGRESS = 2

    /**
     * 活动结束（1小时内，还可以签退、但不能签到）
     */
    const val STUDY_LIVE_FINISH_1_HOUR = 3

    /**
     * 活动结束（超过1小时内，不可以签到/签退）
     */
    const val STUDY_LIVE_FINISH = 4

    /**
     * 线下学习活动状态
     */
    @IntDef(ERROR, STUDY_LIVE_NOT_STARTED, STUDY_LIVE_IN_PROGRESS, STUDY_LIVE_FINISH_1_HOUR, STUDY_LIVE_FINISH)
    @kotlin.annotation.Retention(AnnotationRetention.SOURCE)
    annotation class StudyLiveState

    /**
     * 未签到
     */
    const val SIGN_IN_FALSE = 1

    /**
     * 已签到(活动未结束，已签到，未签退)
     */
    const val SIGN_IN_TRUE = 2

    /**
     * 未签退(活动已结束，已签到，未签退)
     */
    const val SIGN_OUT_FALSE = 3

    /**
     * 已签退
     */
    const val SIGN_OUT_TRUE = 4

    /**
     * 线下学习活动签到状态
     */
    @IntDef(ERROR, SIGN_IN_FALSE, SIGN_IN_TRUE, SIGN_OUT_FALSE, SIGN_OUT_TRUE)
    @kotlin.annotation.Retention(AnnotationRetention.SOURCE)
    annotation class SignState

    /**
     * 发给服务端的key-签到
     */
    const val SERVICE_KEY_SIGN_IN = "1"

    /**
     * 发给服务端的key-签退
     */
    const val SERVICE_KEY_SIGN_OUT = "2"

    /**
     * 获取活动状态类型标识
     */
    @JvmStatic
    @StudyLiveState
    fun getStudyLiveState(timeStart: String?, timeEnd: String?): Int {
        return try {
            if (timeStart.ycIsEmpty() || timeEnd.ycIsEmpty()) {
                ERROR
            } else {
                val current = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
                val currentTime = System.currentTimeMillis()
                val newTimeStart = current.parse(timeStart!!).time
                val newTimeEnd = current.parse(timeEnd!!).time
                when {
                    currentTime < newTimeStart -> {
                        STUDY_LIVE_NOT_STARTED
                    }
                    currentTime < newTimeEnd -> {
                        STUDY_LIVE_IN_PROGRESS
                    }
                    currentTime < (newTimeEnd + 3600000) -> {
                        STUDY_LIVE_FINISH_1_HOUR
                    }
                    else -> {
                        STUDY_LIVE_FINISH
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
            ERROR
        }
    }

    /**
     * 获取活动状态名称
     * @param type Int
     */
    @JvmStatic
    fun getStudyLiveStateName(@StudyLiveState type: Int): String {
        return when (type) {
            STUDY_LIVE_NOT_STARTED -> "未开始"
            STUDY_LIVE_IN_PROGRESS -> "进行中"
            STUDY_LIVE_FINISH_1_HOUR, STUDY_LIVE_FINISH -> "已结束"
            else -> "异常"
        }
    }

    /**
     * 获取活动状态对应图标
     * @param type Int
     */
    @JvmStatic
    @DrawableRes
    fun getStudyLiveStateDrawable(@StudyLiveState type: Int): Int {
        return when (type) {
            STUDY_LIVE_NOT_STARTED -> R.drawable.label_bg_red
            STUDY_LIVE_IN_PROGRESS -> R.drawable.label_bg_yellow
            STUDY_LIVE_FINISH_1_HOUR, STUDY_LIVE_FINISH -> R.drawable.label_bg_green
            else -> R.drawable.label_bg_red
        }
    }

    /**
     * 获取活动状态对应字体颜色
     * @param type Int
     */
    @JvmStatic
    @ColorRes
    fun getStudyLiveStateTvColor(@StudyLiveState type: Int): Int {
        return when (type) {
            STUDY_LIVE_NOT_STARTED -> R.color.state_red
            STUDY_LIVE_IN_PROGRESS -> R.color.state_yellow
            STUDY_LIVE_FINISH_1_HOUR, STUDY_LIVE_FINISH -> R.color.state_green
            else -> R.color.state_red
        }
    }

    /**
     * 获取活动签到状态类型标识
     */
    @JvmStatic
    @SignState
    fun getSignState(signInTime: String?, signOutTime: String?, studyLiveTimeEnd: String?): Int {
        return try {
            if (signInTime.ycIsNotEmpty()) {
                if (signOutTime.ycIsNotEmpty()) {
                    SIGN_OUT_TRUE
                } else {
                    val current = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
                    val currentTime = System.currentTimeMillis()
                    val newTimeEnd = current.parse(studyLiveTimeEnd!!).time
                    if (currentTime < newTimeEnd) {
                        SIGN_IN_TRUE
                    } else {
                        SIGN_OUT_FALSE
                    }
                }
            } else {
                if (signOutTime.ycIsNotEmpty()) {
                    ERROR
                } else {
                    SIGN_IN_FALSE
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
            ERROR
        }
    }

    /**
     * 获取活动签到状态名称
     * @param type Int
     */
    @JvmStatic
    fun getSignStateName(@StudyLiveState type: Int): String {
        return when (type) {
            SIGN_IN_FALSE -> "未签到"
            SIGN_IN_TRUE -> "已签到"
            SIGN_OUT_FALSE -> "未签退"
            SIGN_OUT_TRUE -> "已签退"
            else -> "异常"
        }
    }
}